<?php

// +----------------------------------------------------------------------
// |产品管理控制器
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Cache;
use app\common\Model\StroageModel;
use app\common\Model\ConfigModel;
use think\facade\Db;
use app\common\system\ProductsService;
use app\admin\Model\SupplierModel;
use app\admin\Model\CategoModel;
use app\common\Model\ShopMoedel;
use app\admin\Model\CountryModel;

class Products extends BaseController
{
    protected $Stroage;
     // 构造函数
    public function __construct()
    {
        $this->Stroage = new StroageModel();
    }   
    public function index(){       
         //查询商品供应商
         $supplierfo = (new SupplierModel())->getActivelist();  
          //查询仓库
       
         $storagelist = $this->Stroage->getActivelist();        
        if(request()->isPost()){
            $param = request()->post();
            $data = ProductsService::getlist($param);        
            success($data);    
        }else{           
            View::assign('supplierfo', json_encode($supplierfo));   //全部供应商
            View::assign('is_supplier', json_encode((new SupplierModel())->getActiveStatus())); //状态是正常的供应商
            View::assign('storagelist', json_encode($storagelist));   //全部仓库
            View::assign('is_storage', json_encode($this->Stroage->getActiveStatus())); //状态是正常的仓库
        
            return view::fetch();
        }  
    }
       //采集详情
      public function gather_details(){
        if(request()->isPost()){
            $param = request()->post();
            $data = ProductsService::gather_details($param);        
            success($data);  
         }  
      }
      //获取商品分类数据
      public function  product_list(){   
         if(request()->isPost()){
            $param = request()->post();
            $data = ProductsService::product_list($param);        
            success($data);  
         }  
      }

    //商品添加
    public function create(){
        if(request()->isPost()){
            $param = request()->post();        
            $data = ProductsService::create($param);
            success($data);    
        }
    }
     //商品采集
    public function  gather(){
       if(request()->isPost()){
            $param = request()->post();
            $data = ProductsService::gather($param);        
            success($data);  
        }  
    }

  
   //商品修改上下架的状态
   public function   update_status(){
      if(request()->isPost()){
            $param = request()->post();
            $data= ProductsService::update_status($param);         
            success($data);
      }
   }
 
   //商品详情页
   public function  dateils(){
     //查询商品供应商  
    if(request()->isPost()){
        $param = request()->post();    
        $data= ProductsService::dateils($param);         
        success($data);
      }else{
        $param = request()->get();       
        View::assign('id',$param['id']); 
        return view::fetch();
      }
   }

   //商品分类列表
    public function  catego(){         
        if(request()->isPost()){
            $param = request()->post();
            $data = ProductsService::catego_list($param);        
            success($data);    
        }else{  
            return view::fetch();
        }  
    }
    //获取商品分类数据
    public function catego_info(){
        //查询商品分类
        $field = 'id,catego_info';//要显示的字段
        $where[]=['valid','=',1];//查询条件 
        $where[]=['parecatepubId','=',0];//查询条件      
        $categoinfo = $this->infos(Db::name('catego'),$field,$where); 
        success($categoinfo);  
    }
    

   

    //sku属性列表
    public function attribute(){
        if(request()->isPost()){
            $param = request()->post();
            $data = ProductsService::attribute_list($param);
            success($data);    
        }else{
            return view::fetch();
        }
       
    }    
  
    //商品上下架
     public function shelves(){
           //查配置表的国家
          $config_list=(new CountryModel())->getcountrylist(); 
           //查询店铺
           $shoplist = (new ShopMoedel())->getShoplist(); 
           //查询计量单位
           $field = 'measureid as id,cname,name';//要显示的字段
           $measures = $this->infos(Db::name('measures'),$field,[]); 
           if($measures){
                foreach ($measures as $k => $v) {
                    $measures[$k]['cname'] = $v['cname'] .'('. $v['name']. ')';
                }
           }          
          
         if(request()->isPost()){
            $param = request()->post();
            $data = ProductsService::shelves($param);
            success($data);    
        }else{
            view::assign('countrys',json_encode($config_list));
            View::assign('shoplist', json_encode($shoplist));   
            View::assign('measures', json_encode($measures)); 
            return view::fetch();
        }
    }

   //通过分类获取属性的数据
    public function  attribute_info(){
        if(request()->isPost()){
            $param = request()->post();
            $data = ProductsService::attribute_info($param);
            success($data);    
        }
    }
     //产品上架
    public function  product_listing(){
        if(request()->isPost()){
            $param = request()->post();
            $data = ProductsService::product_listing($param);
            success($data);    
        }
    }

    //修改的或者添加的产品区间价值
    public function price_range(){
        if(request()->isPost()){
            $param = request()->post();
            $data = ProductsService::price_range($param);
            success($data);    
        }
    }
        //敦煌采集
    public function  dhgate(){
        if(request()->isPost()){
            $param = request()->post();
            $data=ProductsService::dhgate($param);
            success($data);
        }else{
            return View::fetch();
        }
    }
    //翻译
    public function  translates(){
        if(request()->isPost()){
            $param = request()->post();         
            $data=translate($param['title']);
            success($data);
        }
    }

       //特性跟卖点翻译，跟产品属性
    public function  features_selling_points_translate(){
        if(request()->isPost()){
            $param = request()->post();           
            $data=ProductsService::features_selling_points_translate($param);            
            
            success($data);
        }
    }
    //计算起批量
    public function  batch(){
        if(request()->isPost()){
            $param = request()->post();
            $data=ProductsService::batch($param);
            success($data);
        }
    }
    
}